#include "Level.h"

CLevel::CLevel(float fWidth, float fLength, CPhysics* pPhysics)
	:m_fLength(fLength), m_fWidth(fWidth), m_pPhysics(pPhysics)
{
	m_uiTexFloor = 0;
	m_uiTexWall = 0;
	//floor
	dCreatePlane(pPhysics->GetSpace(), 0, 1, 0, 0);
	//left wall
	dCreatePlane(pPhysics->GetSpace(), 1, 0, 0, -m_fWidth/2);
	//right wall
	dCreatePlane(pPhysics->GetSpace(), -1, 0, 0, -m_fWidth/2);

	if(!g_bTwoPlayers)
	{
		//top
		dCreatePlane(pPhysics->GetSpace(), 0, 0, 1, -m_fLength/2);
	}
}

CLevel::~CLevel(void)
{
}

int CLevel::Draw()
{

	GLfloat BrownSurface[] = { 0.6f, 0.4f, 0.15f, 1.0f};
	GLfloat WhiteSurface[] = { 1.0f, 1.0f, 1.0f, 1.0f};

	glEnable(GL_TEXTURE_2D);
	
		/* Walls */
	glBindTexture( GL_TEXTURE_2D, m_uiTexWall );

	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, WhiteSurface);

	glBegin(GL_QUADS );		
	glNormal3d( -1.0, 0.0, 0.0);
	glTexCoord2d( m_fLength/4,  0.0); glVertex3d( -m_fWidth/2, 0.0, m_fLength/2);
	glTexCoord2d( m_fLength/4,  0.5); glVertex3d( -m_fWidth/2, 2.0, m_fLength/2);
	glTexCoord2d(         0.0,  0.5); glVertex3d( -m_fWidth/2, 2.0, -m_fLength/2);
	glTexCoord2d(         0.0,  0.0); glVertex3d( -m_fWidth/2, 0.0, -m_fLength/2);
	glEnd();

	glBegin(GL_QUADS);
	glNormal3d( 1.0, 0.0, 0.0);
	glTexCoord2d( m_fLength/4,  0.0); glVertex3d( m_fWidth/2, 0.0, m_fLength/2);
	glTexCoord2d( m_fLength/4,  0.5); glVertex3d( m_fWidth/2, 2.0, m_fLength/2);
	glTexCoord2d(         0.0,  0.5); glVertex3d( m_fWidth/2, 2.0, -m_fLength/2);
	glTexCoord2d(         0.0,  0.0); glVertex3d( m_fWidth/2, 0.0, -m_fLength/2);
	glEnd();

	if(!g_bTwoPlayers)
	{
		glBegin(GL_QUADS);
		glNormal3d( 0.0, -1.0, 0.0);
		glTexCoord2d( m_fWidth/4,  0.0); glVertex3d( -m_fWidth/2, 0.0, -m_fLength/2);
		glTexCoord2d( m_fWidth/4,  0.5); glVertex3d( -m_fWidth/2, 2.0, -m_fLength/2);
		glTexCoord2d(        0.0,  0.5); glVertex3d( m_fWidth/2, 2.0, -m_fLength/2);
		glTexCoord2d(        0.0,  0.0); glVertex3d( m_fWidth/2, 0.0, -m_fLength/2);
		glEnd();
	}

	/* Floor */
	
	glBindTexture( GL_TEXTURE_2D, m_uiTexFloor );

	glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, WhiteSurface);
	glBegin(GL_QUADS);
	glNormal3d( 0.0, 1.0, 0.0);
	glTexCoord2d(-1.0, -1.0); glVertex3d( -m_fWidth/2, 0.0, -m_fLength/2);
	glTexCoord2d(-1.0,  1.0); glVertex3d( -m_fWidth/2, 0.0, m_fLength/2);
	glTexCoord2d( 1.0,  1.0); glVertex3d( m_fWidth/2, 0.0, m_fLength/2);
	glTexCoord2d( 1.0, -1.0); glVertex3d( m_fWidth/2, 0.0, -m_fLength/2);
	glEnd();

	glDisable(GL_TEXTURE_2D);

	return 0;
}

void CLevel::LoadTextures()
{
	m_uiTexFloor = CUtils::LoadRAW("floor.raw", 512, 512);
	m_uiTexWall = CUtils::LoadRAW("wall.raw", 256, 256);
}
